package com.aken.imdemo.util;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.aken.imdemo.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.HashSet;
import java.util.Set;

public class GlideImageGetter implements Html.ImageGetter, Drawable.Callback {

    private final Context mContext;

    private final TextView mTextView;

    private final Set<ImageGetterViewTarget> mTargets;

    private static GlideImageGetter get(View view) {
        return (GlideImageGetter) view.getTag(R.id.drawable_callback_tag);
    }

    GlideImageGetter(Context context, TextView textView) {
        this.mContext = context;
        this.mTextView = textView;

        mTargets = new HashSet<>();
        mTextView.setTag(R.id.drawable_callback_tag, this);
    }

    @Override
    public Drawable getDrawable(String url) {
        final UrlDrawable_Glide urlDrawable = new UrlDrawable_Glide();

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(mContext).load(url).apply(options).into(new ImageGetterViewTarget(mTextView, urlDrawable));

        return urlDrawable;

    }

    @Override
    public void invalidateDrawable(Drawable who) {
        mTextView.invalidate();
    }

    @Override
    public void scheduleDrawable(Drawable who, Runnable what, long when) {

    }

    @Override
    public void unscheduleDrawable(Drawable who, Runnable what) {

    }

    private class ImageGetterViewTarget extends ViewTarget<TextView, Drawable> {

        private final UrlDrawable_Glide mDrawable;

        private ImageGetterViewTarget(TextView view, UrlDrawable_Glide drawable) {
            super(view);
            mTargets.add(this);
            this.mDrawable = drawable;
        }

        @Override
        public void onResourceReady(Drawable resource, Transition<? super Drawable> glideAnimation) {
            Rect rect;
            rect = new Rect(0, 0, 70, 70);
            resource.setBounds(rect);

            mDrawable.setBounds(rect);
            mDrawable.setDrawable(resource);

            mDrawable.setCallback(get(getView()));

            if (resource instanceof GifDrawable) {
                ((GifDrawable) resource).start();
            } else if (resource instanceof Animatable) {
                ((Animatable) resource).start();
            }

            getView().setText(getView().getText());
            getView().invalidate();
        }

        private Request request;

        @Override
        public Request getRequest() {
            return request;
        }


        @Override
        public void setRequest(Request request) {
            this.request = request;
        }
    }
}